package com.ecube.maintenance.components.fragment;

import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ecube.maintenance.R;

import org.apache.commons.io.IOUtils;

import java.io.IOException;

public class ViewUserPolicyFragment extends Fragment {

    TextView mUserPolicyContent;

    public ViewUserPolicyFragment() {
        // Required empty public constructor
    }

    public static ViewUserPolicyFragment newInstance() {
        ViewUserPolicyFragment fragment = new ViewUserPolicyFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        menu.clear();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_view_user_policy, container, false);
        mUserPolicyContent = (TextView) root.findViewById(R.id.user_policy_content);
        try {
            mUserPolicyContent.setText(IOUtils.toString(
                    getActivity().getAssets().open("user_policy.txt")));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  root;
    }

    private ActionBar getActionBar() {
        return getActivity().getActionBar();
    }

    @Override
    public void onResume() {
        super.onResume();
        configureActionBar();
    }

    private void configureActionBar() {
        ActionBar actionBar = getActionBar();
        assert actionBar != null;
        View v = actionBar.getCustomView();
        if (v != null) {
            v.setVisibility(View.GONE);
        }
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle("用户服务协议");
    }
    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden) {
            getFragmentManager().popBackStack();
        } else
            super.onHiddenChanged(hidden);
    }
    @Override
    public void onDetach() {
        super.onDetach();
    }

}
